// 课程管理数据加载

function manageCourse() {
    // 获取分类科目列表
    typeList();
    // 获取年级列表
    gradeList();
    // 添加新增点击事件
    addBtnAction();

    //添加搜索按钮点击事件
    queryBtnAction(pageClickAction);

    // 默认加载第一页数据
    pageClickAction();

    // ajax请求
    function pageClickAction() {

       var data = {
            token: sessionStorage.token,
                page:curPage,
       };
        if ($('#type_id').val()) data.type_id = $('#type_id').val();
        if ($('#grade_id').val()) data.grade_id = $('#grade_id').val();

        $.ajax({
            url: http_url + 'Course/index',
            type: 'POST',
            data: data,
            success: function (response) {
                console.log(response);
                if (response.code == 0) {
                    $('tbody').find('tr').remove();
                    showCourse(response, pageClickAction);
                }
            }

        });
    }
}


function showCourse(response, pageClickAction) {

    createPagiantion(response,pageClickAction);

    var list = response.data.data;
    // list.reverse();

    list.forEach(function (list, index){
        var tr = $('<tr/>').prop('tid',list.id);
        var td_id = $('<td/>').html((curPage - 1) * 10 + index +1);
        var td_name = $('<td/>').html(list.name);
        var td_grade = $('<td/>').html(list.grade);
        var td_type = $('<td/>').html(list.type);
        var td_addtime = $('<td/>').html(timestampToTime(list.addtime));
        var td_start_time = $('<td/>').html(timestampToTime(list.start_time));
        var td_brief = $('<td/>').html(list.brief);
        var td_action = $('<td/>').append($('<a/>').html('编辑 ').prop('href','#').addClass('edit'))
            .append($('<a/>').html('删除').prop('href','#').addClass('delete'));
        tr.append(td_id).append(td_name).append(td_grade).append(td_type).append(td_addtime).append(td_start_time).append(td_brief).append(td_action);
        $('table').append(tr);
    });

//    给编辑添加点击事件
    editBtnCourse();

}

function editBtnCourse() {
    $('.edit').click(function () {
        var id = $(this).parents('tr').prop('tid');
        console.log(id);
        console.log(2222);
        $.ajax({
            url: http_url + 'Course/Course_details?token=' + sessionStorage.token + '&id=' + id,
            type: 'GET',
            success: function (response) {
                console.log(response);
                showCourseInput(response);
            }
        });
        $('#content_wrap').load('addCourse_wy.html', function () {
            editCourse();
        });
    });

    // 编辑课程确定点击事件
    function editCourse(){
        typeList();
        gradeList();
        $(".flatpickr").flatpickr();
        saveBtnaction();
        function saveBtnaction() {
            $('#save').click(function () {
                if (!$('#name').val()) return alert('请输入课程名字');
                if (!$('#type_id').val()) return alert('请选择学科');
                if (!$('#grade_id').val()) return alert('请选择年级');
                if (!$('#brief').val()) return alert('请输入简介');
                if (!$('#start_time').val()) return alert('请选择开课时间');
                $.ajax({
                    url: http_url + 'Course/edit',
                    type: 'POST',
                    data: {
                        token: sessionStorage.token,
                        id: $('#name').prop('tid'),
                        name: $('#name').val(),
                        type_id: $('#type_id').val(),
                        grade_id: $('#grade_id').val(),
                        brief: $('#brief').val(),
                        start_time: Date.parse($('#start_time').val())/1000

                    },
                    success: function (response) {
                        console.log(response);
                        if (response.code == 0) {
                            console.log(response);
                            // console.log(data.start_time);
                            // console.log($('#start_time').val());
                            alert('成功');
                        }
                    }
                });
            });

        }
    }

   function showCourseInput(response) {
        var info = response.data;

        $('#name').val(info.name).prop('tid',info.id);
        $('#type_id').val(info.type_id);
        $('#grade_id').val(info.grade_id);
        $('#brief').val(info.brief);
        $('#start_time').val(timestampToTime(info.start_time));
   }


}

